package com.practice.xml.dom4j;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import com.practice.xml.Company;
import com.practice.xml.CompanyDefinition;
import com.practice.xml.Employee;
import com.practice.xml.EmployeeDefinition;
import com.practice.xml.XmlProcessor;

/**
 * Creates and parses XML document by DOM4J.
 * @author Leonardo
 *
 */
public class Dom4jXmlProcessor implements XmlProcessor {

	@Override
	public void createXml(Company company, String filePath) {
		Document document = DocumentHelper.createDocument();
		
		Element companyElement = document.addElement(CompanyDefinition.NAME);
		companyElement.addAttribute(CompanyDefinition.ATTRIBUTE_NAME, company.getName());
		
		Element employeesElement = companyElement.addElement(CompanyDefinition.ELEMENT_EMPLOYEES);
		for (Employee employee : company.getEmployees()) {
			Element employeeElement = employeesElement.addElement(EmployeeDefinition.NAME);
			
			Element name = employeeElement.addElement(EmployeeDefinition.ELEMENT_NAME);
			name.setText(employee.getName());
			
			Element sex = employeeElement.addElement(EmployeeDefinition.ELEMENT_SEX);
			sex.setText(Boolean.toString(employee.getSex()));
			
			Element age = employeeElement.addElement(EmployeeDefinition.ELEMENT_AGE);
			age.setText(Integer.toString(employee.getAge()));
		}
		
		
		try {
			Writer fileWriter = new FileWriter(filePath);
			XMLWriter xmlWriter = new XMLWriter(fileWriter);
			xmlWriter.write(document);
			xmlWriter.close();
		} catch (IOException ex) {
			System.out.println(ex.getMessage());
			System.out.println(ex.getStackTrace());
		}
	}

	@Override
	public Company parseXml(String filePath) {
		Document document = null;
		try {
			Reader fileReader = new FileReader(filePath);
			SAXReader saxReader = new SAXReader();			
			document = saxReader.read(fileReader);	
		} catch (FileNotFoundException ex) {
			System.out.println(ex.getMessage());
			System.out.println(ex.getStackTrace());
		} catch (DocumentException ex) {
			System.out.println(ex.getMessage());
			System.out.println(ex.getStackTrace());			
		}
		
		Element companyElement = document.getRootElement();
		Company company = new Company();
		
		Attribute nameAttribute = companyElement.attribute(CompanyDefinition.ATTRIBUTE_NAME);
		company.setName(nameAttribute.getText());

		Element employeesElement = companyElement.element(CompanyDefinition.ELEMENT_EMPLOYEES);
		List<Employee> employees = new ArrayList<>();
		company.setEmployees(employees);
		
		for (Object empObj : employeesElement.elements(EmployeeDefinition.NAME)) {
			Element employeeElement = (Element) empObj;
			Employee employee = new Employee();
			
			Element nameELement = employeeElement.element(EmployeeDefinition.ELEMENT_NAME);
			employee.setName(nameELement.getText());
			
			Element sexElement = employeeElement.element(EmployeeDefinition.ELEMENT_SEX);
			employee.setSex(Boolean.parseBoolean(sexElement.getText()));
			
			Element ageElement = employeeElement.element(EmployeeDefinition.ELEMENT_AGE);
			employee.setAge(Integer.parseInt(ageElement.getText()));
			
			employees.add(employee);
		}
		
		return company;
	}
	
}
